package com.yc.mybatis.mapper;

import com.yc.mybatis.bean.SpPlan;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface PlanMapper {

    @Select("select * from sp_plan where id =#{id}")
    // Results ==> <resultMap> id ==> id value ==> 每个字段映射
    @Results( id = "rm2",
    value = {
            // 主键映射
            @Result( column = "id", property = "id", id = true),
            // 自定义字段映射
            @Result( column = "play_time", property = "playTime"),
            // 一对一关联
            @Result( column = "hid", property = "hall",
                    one = @One(select = "selectById", fetchType = FetchType.LAZY))

    })
    public SpPlan selectById(Integer id);

    @Select("select * from sp_plan where mid =#{mid} and hid =#{hid}")
    public List<SpPlan> selectByMidAndHid(
            @Param("mid") Integer mid,
            @Param("hid") Integer hid);


    @Select("select\n" +
            "            a.id id,\n" +
            "            a.play_time playTime,\n" +
            "            a.mid,\n" +
            "            a.hid,\n" +
            "            a.price,\n" +
            "            b.id \"hall.id\",\n" +
            "            b.name \"hall.name\",\n" +
            "            b.is3d \"hall.is3d\",\n" +
            "            b.size \"hall.size\",\n" +
            "            b.seats \"hall.seats\"\n" +
            "        from sp_plan a join sp_hall b on a.hid=b.id where mid=#{mid}")
    @ResultMap("rm2")
    public List<SpPlan> selectByMid(int mid);
}
